/*=============================================================================================================
 * InputFragment.java
 *
 * 1.프로젝트 : ToYou - 명함관리 어플
 * 2.패 키 지 : com.trillion.toyou
 * 3.작업환경 : Android
 * 2.작 업 자 : 
 * 3.작업일자 : 2014.12.09
 * 4.내    용 : 명함내용을 입력하는 프래그먼트 화면
 * 5.수 정 자 : -----
 * 6.수정일자 : -----
 * 7.수정내용 : -----
 * =============================================================================================================*/
package com.trillion.toyou;

import java.util.ArrayList;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class InputFragment extends Fragment implements OnClickListener{
	View view = null;
	
	ImageButton bt_input = null;
	ImageButton bt_cancel = null;
	ImageButton bt_back = null;
	
	LinearLayout topTitle = null;
	InputMethodManager imm = null;
	
	TextView nname = null;
	TextView grade = null;
	TextView company = null;
	TextView email= null;
	TextView telNum = null;

	TextView phoneNum = null;
	TextView address = null;
	TextView department= null;

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.view = inflater.inflate(R.layout.input_layout, container, false);
		
		MainActivity.page_check = new MainActivity().BACK_OK;
		
		bt_input = (ImageButton)view.findViewById(R.id.input_layout_bt_input);
		bt_cancel = (ImageButton)view.findViewById(R.id.input_layout_bt_cancel);
		bt_back = (ImageButton)view.findViewById(R.id.input_layout_bt_back2);
		imm = (InputMethodManager)getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
		
		nname = (TextView)view.findViewById(R.id.input_layout_et_nname);
		grade = (TextView)view.findViewById(R.id.input_layout_et_grade);
		company = (TextView)view.findViewById(R.id.input_layout_et_company);
		email= (TextView)view.findViewById(R.id.input_layout_et_email);
		telNum = (TextView)view.findViewById(R.id.input_layout_et_telNum);

		phoneNum = (TextView)view.findViewById(R.id.input_layout_et_phoneNum);
		address = (TextView)view.findViewById(R.id.input_layout_et_address);
		department= (TextView)view.findViewById(R.id.input_layout_et_department);

		
		this.topTitle = (LinearLayout)this.view.findViewById(R.id.input_layout_top);
		
		
		bt_input.setOnClickListener(this);
		bt_cancel.setOnClickListener(this);
		bt_back.setOnClickListener(this);
		
		return view;
	}
	
	@Override
	public void onResume() {
		new NfcOnOffChange(this.topTitle, R.drawable.top_bar_on_nfc_my, R.drawable.top_bar_off_nfc_my);
		super.onResume();
	}
	
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		Fragment frag = null;
		FragmentTransaction trans = null;
		
		switch(id)
		{
		case R.id.input_layout_bt_input :
			boolean b = inputinput();
			
			if(b){
				trans = getFragmentManager().beginTransaction();
				frag = new MyFragment();
				trans.replace(R.id.root_layout, frag);
				trans.addToBackStack(null);
				trans.commitAllowingStateLoss();
				
				Toast.makeText(getActivity(), "명함이 정상적으로 저장되었습니다.", Toast.LENGTH_SHORT);
			}else{
				Toast.makeText(getActivity(), "저장 중 오류가 발생했습니다.", Toast.LENGTH_SHORT);
			}
			
			
			break;
		case R.id.input_layout_bt_cancel :
			if(MainActivity.my_main_namecard == null){
				trans = getFragmentManager().beginTransaction();
				frag = new ListFragment();
				trans.replace(R.id.root_layout, frag);
				trans.addToBackStack(null);
				trans.commitAllowingStateLoss();
				
			}else{
				trans = getFragmentManager().beginTransaction();
				frag = new MyFragment();
				trans.replace(R.id.root_layout, frag);
				trans.addToBackStack(null);
				trans.commitAllowingStateLoss();
			}
			break;
		case R.id.input_layout_bt_back2:
			
			imm.hideSoftInputFromWindow(company.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(nname.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(grade.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(department.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(address.getWindowToken(), 0);

			imm.hideSoftInputFromWindow(telNum.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(phoneNum.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(email.getWindowToken(), 0);

			
			trans = getFragmentManager().beginTransaction();
			frag = new MyFragment();
			trans.replace(R.id.root_layout, frag);
			trans.addToBackStack(null);
			trans.commitAllowingStateLoss();
			break;
		}

	}
	
	public boolean inputinput()
	{
		boolean b = false;
		if(MainActivity.my_main_namecard.getNid() == 0){
			DBHelper db = new DBHelper(getActivity(), "toyou", null, 1);
			Data data = new Data();

			TextView nname = (TextView)view.findViewById(R.id.input_layout_et_nname);
			TextView grade = (TextView)view.findViewById(R.id.input_layout_et_grade);
			TextView company = (TextView)view.findViewById(R.id.input_layout_et_company);
			TextView email= (TextView)view.findViewById(R.id.input_layout_et_email);
			TextView telNum = (TextView)view.findViewById(R.id.input_layout_et_telNum);

			TextView phoneNum = (TextView)view.findViewById(R.id.input_layout_et_phoneNum);
			TextView address = (TextView)view.findViewById(R.id.input_layout_et_address);
			TextView department= (TextView)view.findViewById(R.id.input_layout_et_department);
			
			
			if(nname.getText().toString().equals("")){
				nname.setText("디폴트");
				grade.setText("디폴트직급");
				company.setText("디폴트회사");
				email.setText("default@default.com");
				telNum.setText("000-0000-0000");
				phoneNum.setText("111-1111-1111");
				address.setText("디폴트 주소");
				department.setText("디폴트부서");
				
			}
			
			
			
			data.setNname(nname.getText().toString());
			data.setGrade(grade.getText().toString());
			data.setCompany(company.getText().toString());
			data.setEmail(email.getText().toString());
			data.setTelNum(telNum.getText().toString());
			
			data.setPhoneNum(phoneNum.getText().toString());
			data.setAddress(address.getText().toString());
			data.setDepartment(department.getText().toString());
			
			db.insertMine(data);
			
			ArrayList<Data> tempAl = db.selectMine();
			
			data = tempAl.get(0);
			
			b = db.modifyMineTitle(data.getNid());
			
			MainActivity.my_main_namecard = data;
			
		}else{
			DBHelper db = new DBHelper(getActivity(), "toyou", null, 1);
			Data data = new Data();
			
			if(company.getText().toString().equals(""))
			{
				data.setCompany(company.getText().toString());
				company.requestFocus();
				imm.showSoftInput(company, InputMethodManager.SHOW_FORCED);
				Toast.makeText(getActivity(), "회사를 입력해주세요.", Toast.LENGTH_SHORT).show();
				
			}
			else if(grade.getText().toString().equals(""))
			{
				data.setGrade(grade.getText().toString());
				grade.requestFocus();
				imm.showSoftInput(grade, InputMethodManager.SHOW_FORCED);
				Toast.makeText(getActivity(), "직급을 입력해주세요.", Toast.LENGTH_SHORT).show();
			}
			else if(department.getText().toString().equals(""))
			{
				data.setDepartment(department.getText().toString());
				department.requestFocus();
				imm.showSoftInput(department, InputMethodManager.SHOW_FORCED);
				Toast.makeText(getActivity(), "부서를 입력해주세요.", Toast.LENGTH_SHORT).show();
			}
			else if(nname.getText().toString().equals(""))
			{
				data.setNname(nname.getText().toString());
				nname.requestFocus();
				imm.showSoftInput(nname, InputMethodManager.SHOW_FORCED);
				Toast.makeText(getActivity(), "이름을 입력해주세요.", Toast.LENGTH_SHORT).show();
			}
			else if(address.getText().toString().equals(""))
			{
				data.setAddress(address.getText().toString());
				address.requestFocus();
				imm.showSoftInput(address, InputMethodManager.SHOW_FORCED);
				Toast.makeText(getActivity(), "주소를 입력해주세요.", Toast.LENGTH_SHORT).show();
			}
			else if(telNum.getText().toString().equals(""))
			{
				data.setTelNum(telNum.getText().toString());
				telNum.requestFocus();
				imm.showSoftInput(telNum, InputMethodManager.SHOW_FORCED);
				Toast.makeText(getActivity(), "회사번호를 입력해주세요.", Toast.LENGTH_SHORT).show();
			}
			else if(phoneNum.getText().toString().equals(""))
			{
				data.setPhoneNum(phoneNum.getText().toString());
				phoneNum.requestFocus();
				imm.showSoftInput(phoneNum, InputMethodManager.SHOW_FORCED);
				Toast.makeText(getActivity(), "핸드폰번호를 입력해주세요.", Toast.LENGTH_SHORT).show();
			}
			else if(email.getText().toString().equals(""))
			{
				data.setEmail(email.getText().toString());
				email.requestFocus();
				imm.showSoftInput(email, InputMethodManager.SHOW_FORCED);
				Toast.makeText(getActivity(), "이메일을 입력해주세요.", Toast.LENGTH_SHORT).show();
			}
			else{
				data.setNname(nname.getText().toString());
				data.setGrade(grade.getText().toString());
				data.setCompany(company.getText().toString());
				data.setEmail(email.getText().toString());
				data.setTelNum(telNum.getText().toString());
				
				data.setPhoneNum(phoneNum.getText().toString());
				data.setAddress(address.getText().toString());
				data.setDepartment(department.getText().toString());
			
				b = db.insertMine(data);	
			}
		}
		
		
		
	 	return b;
	}
	
	
	
}
